package com.lyf.emall.controller;

import com.lyf.emall.bean.ResponseResult;
import com.lyf.emall.bean.SysUser;
import com.lyf.emall.service.SysService;
import com.lyf.emall.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @Description:系统controller
 * @Author Lfy
 * @Date 2022/2/15-13:39
 */
@RestController
public class SysController {

    @Autowired
    private SysService sysService;

    @PostMapping("/sys/login")
    public ResponseResult login(@RequestParam String userName,
                                @RequestParam String password){
        SysUser user = new SysUser(userName, password);
        return sysService.login(user);
    }

    @GetMapping("/sys/logout")
    @PreAuthorize("hasAuthority('/sys/logout')")
    public ResponseResult logout(){
        ResponseResult logout = sysService.logout();
        return logout;
    }

    @GetMapping("/sys/getUser")
    @PreAuthorize("hasAuthority('/sys/getUser')")
    public ResponseResult getUserInfo(@RequestHeader("token") String token) throws Exception {
        // 解析token
        Claims claims = JwtUtil.parseJWT(token);
        String userId = claims.getSubject();

        return sysService.getUserInfo(Integer.valueOf(userId));
    }
}
